const express = require('express');

const db = require('../../db')

const router = express.Router();

// ---------------------------------------------获取汽车品牌---------------------------------------------------
router.get('/get_car_brand', (req, res) => {
    let indexList = []
    let brand_list = []
    db('select distinct first_letter from car_brand', (err, data) => {
        if (err) {
            throw err
        }
        data.forEach(item => {
            indexList.push(item.first_letter)
            brand_list.push([]);
        });
        indexList.sort();
        db('select * from car_brand', (err, brandList) => {
            if (err) {
                throw err
            }

            brandList.forEach((brandItem) => {
                let index = indexList.indexOf(brandItem.first_letter)
                if (index !== -1) {
                    brand_list[index].push(brandItem)
                }
            })

            // console.log(brand_list);
            res.send({
                code: 1,
                msg: '获取成功！',
                data: {
                    indexList,
                    brand_list

                }
            })

        })
    })
})


// ---------------------------------------------获取汽车车型---------------------------------------------------
router.get('/get_car_model', (req, res) => {
    const { brand_id } = req.query;
    db(`select * from car_model where brand_id = ${brand_id}`, (err, modelList) => {
        if (err) {
            throw err
        }
        res.send({
            code: 1,
            msg: '获取成功！',
            data: modelList
        })

    })
})



// ---------------------------------------------获取所有汽车数据---------------------------------------------------

router.get('/get_car', (req, res) => {
    /* 参数说明：
      * @param {Number} tag_types [tag_types = 0] - 必须 排序方式 0 智能排序 1 价格最低 2 价格最高 3 座位最多
      * @param {Number}model_id - 非必须 车品牌型号ID
      * @param {Object}price - 非必须 价格范围 
      * @param {Number}seating - 非必须 座位数  
      * @param {Number}car_model - 非必须 车型 0.SUV 1.SRV 2.CRV 3.CUV 4.RV 5.HRV 6.RAV 7.MPV 9.NCV
      * @param {Number}drive - 非必须 驱动方式 0.前驱 1.后驱 2.四驱
      * @param {Number}cylinder - 非必须 气缸数（个）
    */
    const { tag_types, model_id, price, seating, car_model, energy, limit, page } = req.query;
    console.log(req.query);
    let sql = 'select * from car_info'
    let start, sort_by;
    let stringArr=[]
    //车品牌型号ID
    if (model_id || price || seating || car_model || energy ) {
        sql += ` where`;
        if (model_id) {
            stringArr.push(` model_id=${model_id} `) ;
        }
        if (seating) {
            stringArr.push(` seating=${seating} `);
        }
        if (car_model>=0) {
            stringArr.push(` car_model=${car_model} `);
        }
        if (energy) {
            stringArr.push(` energy='${energy}' `);
        }
    }
    sql+=stringArr.join(' && ')
    // 获取排序方式 
    if (tag_types==1) {
        sort_by=' order by price asc'
    }else if(tag_types==2){
        sort_by=' order by price desc'
    }else if(tag_types==3){
        sort_by=' order by seating desc'
    }else{
        sort_by=''
    }
    sql += sort_by;
    // 获取分页
    if (limit && page) {
        start = ` limit ${(page - 1) * limit},${limit}`
        sql += start;
    }
    console.log(sql);
    db(sql, (err, data) => {
        if (err) {
            throw err
        }
        res.send({
            code: 1,
            msg: '获取成功！',
            data
        })

    })
})



module.exports = router